package com.dpteam.service.impl;

import com.dpteam.bean.NoticeInfo;
import com.dpteam.dao.NewsDao;
import com.dpteam.model.News;
import com.dpteam.service.NewsService;
import com.dpteam.util.Page;
import com.dpteam.util.TransformUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by YangZhi on 2016/1/12.
 */
@Service
@Transactional
public class NewsServiceImpl implements NewsService {

    @Resource
    private NewsDao newsDao;

    @Override
    public int addNews(News news) {
        return (int)newsDao.save(news);
    }

    @Override
    public void deleteNewsById(int newsId) {
        newsDao.deleteById(newsId);
    }

    @Override
    public List<NoticeInfo> findAll() {
        List<News> list = newsDao.findAll();
        List<NoticeInfo> noticeInfos = new ArrayList<>();
        for (News news : list) {
            noticeInfos.add(TransformUtil.fromNews(news));
        }
        return noticeInfos;
    }

    @Override
    public List<NoticeInfo> findAll(Page page) {
        List<News> list = newsDao.findAllByPage(page);
        List<NoticeInfo> noticeInfos = new ArrayList<>();
        for (News news : list) {
            noticeInfos.add(TransformUtil.fromNews(news));
        }
        return noticeInfos;
    }
}
